﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace Xm.CustomDesigner.UI.Adorners
{
    internal class XmLineAdorner : Adorner
    {
        //父容器canvas
        private readonly XmCanvas _canvas;

        private readonly PathFigure _pathFigure;//线条的路径骨架

        private readonly PathGeometry _pathGeometry;//线条的路径几何图形

        private readonly BezierSegment _bezierSegment;//贝塞尔曲线段

        private readonly Pen _pen;//线条的画笔

        internal XmNode HostNode { get; set; } //线条的宿主节点

        internal XmNode? AttachNode { get; set; }//线条的附加节点


        public XmLineAdorner(XmCanvas adornedCanvas, XmNode host) : base(adornedCanvas)
        {
            _canvas = adornedCanvas;
            _pathFigure = new PathFigure();
            _pathGeometry = new PathGeometry();
            _bezierSegment = new BezierSegment();
            _pathFigure.Segments.Add(_bezierSegment);//添加曲线段
            _pathGeometry = new PathGeometry(new PathFigure[] { _pathFigure });//定义路径几何图形
            _pen = new Pen(Brushes.Black, 2) { LineJoin = PenLineJoin.Round };

            this.HostNode = host;
        }


        /// <summary>
        /// 重写OnRender方法，实现自定义绘制
        /// </summary>
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            //绘制线条
            drawingContext.DrawGeometry(Brushes.Transparent, _pen, _pathGeometry);
            drawingContext.DrawRectangle(Brushes.Transparent, _pen, new Rect(this.RenderSize));//绘制矩形

        }

        /// <summary>
        /// 鼠标移动的逻辑
        /// </summary>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.RightButton == MouseButtonState.Pressed)
            {
                this.OnChanged(e.GetPosition(this));
                this.InvalidateVisual();//重绘
            }
            else
            {
                if (this.IsMouseCaptured)
                {
                    this.ReleaseMouseCapture();
                }
            }
        }

        /// <summary>
        /// 鼠标抬起的逻辑--连接
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
        {

            if (this.IsMouseCaptured)
            {
                this.ReleaseMouseCapture();
            }


            if (_canvas.InputHitTest(e.GetPosition(this)) is UIElement element)
            {
                var node = element.FindParent<XmNode>();
                if (node != null)
                {
                    this.AttachNode = node;
                    this.OnChanged(e.GetPosition(this));
                    this.InvalidateVisual();//重绘
                }
            }

            if (AttachNode != null)
            {
                var line = new XmLine
                {
                    //error---不能赋值，要不然不走line里边的创建逻辑了
                    //SourceNode = this.HostNode,
                    //TargetNode = this.AttachNode,

                    SourceId = this.HostNode.Name,
                    TargetId = this.AttachNode.Name,
                };

                if (line != null)
                {
                    _canvas.Children.Add(line);                   
                }               
            }

            var layer = AdornerLayer.GetAdornerLayer(_canvas);

            if (layer != null)
            {
                layer.Remove(this);
            }
        }


        /// <summary>
        /// 在canvas中绘制线条
        /// </summary>
        private void OnChanged(Point mouse)
        {
            var start = this.HostNode.Right;
            var end = mouse;
            var p1 = new Point(start.X + 30, start.Y);
            var p2 = new Point(end.X - 30, end.Y);


            if (this.HostNode.Right.X < mouse.X)
            {
                start = this.HostNode.Right;
            }
            else if (this.HostNode.Right.X > mouse.X)
            {
                start = this.HostNode.Left;
                end = mouse;
                p1.X = start.X - 30;
                p2.X = end.X + 30;
            }

            //(重新)绘制曲线
            _pathFigure.StartPoint = start;
            _bezierSegment.Point1 = p1;
            _bezierSegment.Point2 = p2;
            _bezierSegment.Point3 = end;
        }
    }
}
